import com.troyworks.framework.ui.layout.Bounds2;
import com.troyworks.datastructures.Array2;
import com.troyworks.framework.ui.MCSort;
/**
 * @author Troy Gardner
 */
class com.troyworks.particleengine.DockPrototype extends MovieClip {

	var clips : Array2;
	var cbn : Bounds2;
	var isHit : Boolean = false;
	public var container_mc : MovieClip;
	public var clips_mc : MovieClip;
	public var d : MovieClip;
	public var pad : Number = 10;
	public var placeholder : MovieClip;

	public function DockPrototype() {
		clips  = new Array2();
	
	}
	public function onLoad() : Void{
		cbn = new Bounds2(container_mc.getBounds(this));
		trace("clips_mc " + clips_mc);
		clips.push(clips_mc.c);
		clips.push(clips_mc.a);
		clips.push(clips_mc.b);
		
		placeholder = clips_mc.placeholder;
		placeholder._visible = false;
	}
	function setIsHit(val : Boolean, db : Bounds2, cb : Bounds2) : Void {
		if (val != isHit) {
			isHit = val;
			if (isHit) {
				var bb : Bounds2 = new Bounds2(d.getBounds(clips_mc));
				placeholder._x = bb.xMin;
				placeholder._y = bb.yMin;
				placeholder._width = bb.width;
				placeholder._height = bb.height;
				placeholder._visible = true;
				
				clips.push(placeholder);
				clips.sort(MCSort.order_X);
				onHitEnter(db, cb);
			} else {
				clips.remove(placeholder);
				placeholder._visible = false;
				onHitLeave(db, cb);
				
				
			}
		}else if (isHit){
					var bb : Bounds2 = new Bounds2(d.getBounds(clips_mc));
				placeholder._x = bb.xMin;
				placeholder._y = bb.yMin;
				placeholder._width = bb.width;
				placeholder._height = bb.height;
				placeholder._visible = true;
				clips.sort(MCSort.order_X);
				
		
		}
	}
	function onHitEnter(db : Bounds2, cb : Bounds2) : Void {
		if (db.xMax<cb.xMid) {
			trace("to the left");
			//if(db.xMin <= container_mc._x){
			//container_mc._x = db.xMin - pad;
			container_mc._width = cbn.width+db.width+pad;
			//}
		} else if (cb.xMid<db.xMin) {
			trace("to the right");
			if (cb.xMax<db.xMax) {
				//				container_mc._x = db.xMin - pad;
				container_mc._width = cbn.width+db.width+pad;
			}
		} else if (cb.xMin<db.xMin && db.xMax<cb.xMax) {
			trace("in middle");
			//				if(db.xMin <= container_mc._x){
			//container_mc._x = db.xMin - pad;
			container_mc._width = cbn.width+db.width+2*pad;
			//		}
		}
	}
	function onHitLeave(db : Bounds2, cb : Bounds2) : Void {
		container_mc._width = cbn.width;
	}
	function onEnterFrame() : Void {
		var pad : Number = 10;
		var cx : Number = pad;
		var db : Bounds2 = new Bounds2(d.getBounds(this));
		var cb : Bounds2 = new Bounds2(container_mc.getBounds(this));
		if (d.hitTest(container_mc)) {
			trace("hit");
			setIsHit(true, db, cb);
		} else {
			trace("no hit");
			setIsHit(false, db, cb);
		}
		for (var i : Number = 0; i<clips.length; i++) {

			var _mc : MovieClip = MovieClip(clips[i]);
			trace("layout _mc " + _mc);

			_mc._x = cx;
			_mc._y = 0;
			// = container_mc._y;
			//pad;
			cx += _mc._width+pad;
		}
	}

	
}